package com.hxrfid.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.system.domain.bo.SysTemplateBo;
import com.hxrfid.system.domain.vo.SysTemplateVo;
import com.hxrfid.system.service.ISysTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 权限模板控制器
 * 前端访问路由地址为:/system/template
 *
 * @author ruoyi
 * @date 2022-11-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/template")
public class SysTemplateController extends BaseController {

    private final ISysTemplateService iSysTemplateService;

    /**
     * 查询权限模板列表
     */
    @SaCheckPermission("system:template:list")
    @GetMapping("/list")
    public TableDataInfo<SysTemplateVo> list(SysTemplateBo bo, PageQuery pageQuery) {
        return iSysTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出权限模板列表
     */
    @SaCheckPermission("system:template:export")
    @Log(title = "权限模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysTemplateBo bo, HttpServletResponse response) {
        List<SysTemplateVo> list = iSysTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "权限模板", SysTemplateVo.class, response);
    }

    /**
     * 获取权限模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:template:query")
    @GetMapping("/{id}")
    public R<SysTemplateVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iSysTemplateService.queryById(id));
    }

    /**
     * 新增权限模板
     */
    @SaCheckPermission("system:template:add")
    @Log(title = "权限模板", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTemplateBo bo) {
        return toAjax(iSysTemplateService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改权限模板
     */
    @SaCheckPermission("system:template:edit")
    @Log(title = "权限模板", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTemplateBo bo) {
        return toAjax(iSysTemplateService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除权限模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:template:remove")
    @Log(title = "权限模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysTemplateService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
